﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xiaodingmarket_Domain.Interfaces.IRepository;
using xiaodingmarket_Domain.Models;
using xiaodingmarket_Infra_Data.Context;
using static xiaodingmarket_Domain.Models.EunmType;

namespace xiaodingmarket_Infra_Data.Repository
{
    public class CommodityRepository :Repository<Commodity>, ICommodityRepository
    {
        public CommodityRepository(MarketContext context):base(context)
        {

        }

        public IQueryable<Commodity> Get(int num, int commoditystate)
        {
            return  DbSet.Include(c => c.CommodityDetail) //饥饿加载
                .AsNoTracking()   //不跟踪
                .Where(c => c.State == Commoditystate.reviewing && c.IsDelete == false)
                .Take(num);   //分页
        }

        public IQueryable<Commodity> GetAll(int kind)
        {
            return DbSet.Include(c => c.CommodityDetail).Where(c => c.Kind == (Kind)kind && c.IsDelete == false);
        }

        public List<Commodity> GetAny(List<Guid> commodityid)
        {
            List<Commodity> data = new List<Commodity>();
            foreach (Guid id in commodityid)
            {
                var d = DbSet.Include(c => c.CommodityDetail).Single(c => c.Id == id && c.IsDelete == false);

                data.Add(d);

            }
            return data;

        }

        public IQueryable<Commodity> GetAnyByCategory(int category)
        {
            return DbSet.Include(c => c.CommodityDetail).Where(c => c.Category == (Category)category && c.IsDelete == false);
        }

        public IQueryable<Commodity> GetAnyByRemark(string remark)
        {
            return DbSet.Include(c => c.CommodityDetail).Where(c => c.Remark == remark&&c.IsDelete==false);
        }

        public Commodity GetById(Guid id, Guid userid)
        {
            return DbSet.Include(c => c.CommodityDetail).Single(c => c.Id == id && c.CommodityDetail.StudentId == userid && c.IsDelete == false);
        }
        public Commodity GetOneById(Guid id)
        {
            return DbSet.Include(c => c.CommodityDetail).Single(c => c.Id == id && c.IsDelete == false);
        }
    }
}
